/*
 * File:   Object.h
 * Author: Julian Sanin
 * Created on 09/02/2014, 10:49
 * Ported "C+" to PIC24.
 * Modified the naming and removed a warning in _Object_ctor().
 */

/*****************************************************************************
* Product:  "C+" Object Oriented Programming in C
* Version:  3.0
* Updated:  January 29, 2005
*
* Copyright (C) 2002-2005 Quantum Leaps. All rights reserved.
*
* This software may be distributed and modified under the terms of the
* BSD open source license, which is provided below.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* Neither the name of the Quantum Leaps nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL QUANTUM LEAPS OR OTHER CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* Contact information:
* Quantum Leaps Web site:  http://www.quantum-leaps.com
* Quantum Leaps licensing: http://www.quantum-leaps.com/licensing
* e-mail:                  sales@quatnum-leaps.com
*****************************************************************************/

#ifndef OBJECT_H
#define	OBJECT_H

#ifdef	__cplusplus
extern "C" {
#endif

#include <stddef.h>

/** Declaration of a class. */
#define CLASS(className) \
    typedef struct className##_t className; \
    struct className##_t {
/** Declaration of methods of a class. */
#define METHODS };
/** End of declaration of a class. */
#define CLASS_END

CLASS(Object)
    struct _ObjectVtbl_t *_vptr;
METHODS
    #define _Object_ctor(obj) ((obj)->_vptr = &_cObjectVtbl)
    #define _Object_dtor(obj) ((void)0)
    /** Implementation of pure virtual method. */
    void Object_virtual(void);
    /** Run Time Type Identification. */
    #define INSTANCE_OF(obj, className) \
        _Object_instanceOf((Object *)(obj), &_c##className##Vtbl)
    int _Object_instanceOf(const Object *obj, const void *vtbl);
CLASS_END

CLASS(_ObjectVtbl)
    _ObjectVtbl *_super; /* Pointer to superclass vtable. */
    void (*dtor)(Object *); /* Public virtual destructor. */
METHODS
CLASS_END

extern _ObjectVtbl _cObjectVtbl; /* Object class descriptor. */

/** Declaration of a subclass. */
#define CLASS_EXTEND(className, superClass) \
    CLASS(className) \
        superClass super;

/** Declaration of overridden methods of a class. */
#define OVERRIDE(className, superClass) }; \
    typedef struct _##className##Vtbl_t _##className##Vtbl; \
    extern _##className##Vtbl _c##className##Vtbl; \
    struct _##className##Vtbl_t { \
        _##superClass##Vtbl _super;

#define VTABLE(className, superClass) \
    _##className##Vtbl _c##className##Vtbl; \
    static _ObjectVtbl *_##className##VtblCtor(className *t) { \
        _##className##Vtbl *obj = &_c##className##Vtbl; \
        *(_##superClass##Vtbl *)obj = \
            *(_##superClass##Vtbl *)((Object *)t)->_vptr;

#define VMETHOD(className, method) ((_##className##Vtbl *)obj)->method

#define VTABLE_END \
        ((_ObjectVtbl *)obj)->_super = ((Object *)t)->_vptr; \
        return (_ObjectVtbl *)obj; \
    }

#define VHOOK(className) \
    if(((_ObjectVtbl *)&_c##className##Vtbl)->_super == NULL) \
        ((Object *)obj)->_vptr = _##className##VtblCtor(obj); \
    else \
        (((Object *)obj)->_vptr = (_ObjectVtbl *)&_c##className##Vtbl)

/** Invoke virtual method.
 *  Insert additional parameters by a comma seperated list.
 *  Terminate call with VIRTUAL_END. */
#define VIRTUAL(className, method, obj) \
    (*((_##className##Vtbl *)(((Object *)(obj))->_vptr))->method) \
        ((className *)(obj)
#define VIRTUAL_END );

#ifdef	__cplusplus
}
#endif

#endif	/* OBJECT_H */
